package com.woniu.bank.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.bank.dto.*;
import com.woniu.bank.entity.ClientInfoSheet;
import com.woniu.bank.entity.Member;
import com.woniu.bank.service.ClientInfoService;
import com.woniu.bank.service.MemberService;
import com.woniu.bank.util.Result;
import com.woniu.bank.vo.ClientInfoSheetRatedVO;
import com.woniu.bank.vo.MemberClientInfoSheetVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：zhuboyuan
 * @date ：Created in 2023/1/4 16:42
 * @description：c{description}
 * @modified By：
 * @version: $
 */
@RestController
@RequestMapping("members")
@Api(tags = "联保成员接口", description = "所有的联保成员接口")
public class MemberController {

    @Autowired
    private MemberService memberService;
    @Autowired
    private ClientInfoService clientInfoService;

    @ApiOperation(value = "查询所有联保成员信息",notes ="查询全部成员",httpMethod="POST")
    @PostMapping("selectMember")
    public Result selectMember(@RequestBody MemberDTO memberDTO) {
        List<MemberClientInfoSheetVO> lists = memberService.selectMember(memberDTO);
        PageInfo<MemberClientInfoSheetVO> memberClientInfoSheetVOPageInfo = new PageInfo<>(lists);
        if(memberClientInfoSheetVOPageInfo==null){
            return Result.error("查询失败");
        }
        return Result.ok().data("lists",memberClientInfoSheetVOPageInfo);
    }


    @PutMapping("deleteMember")
    @ApiOperation(value = "联保成员软删操作", notes = "联保成员根据id软删除联保成员的数据", httpMethod = "PUT")
    public Result deleteMember(@RequestBody MemberIsDeleteDTO memberIsDeleteDTO){

        int i = memberService.updateIsDelete(memberIsDeleteDTO);
        return Result.ok();
    }




    @Transactional
    @PostMapping("addAllMember")
    @ApiOperation(value = "联保成员群体添加操作", notes = "联保成员群体添加操作", httpMethod = "POST")
    public Result addMember(@RequestBody String[] nums, MxzDTO mxzDTO) {
        for (String num : nums) {
            NewClientfoDTO newClientfoDTO = new NewClientfoDTO();
            newClientfoDTO.setClientNumber(num);
            List<ClientInfoSheetRatedVO> clientInfoSheetRatedVOS = clientInfoService.seleteAddKeHu(newClientfoDTO);
            System.out.println("=================================================="+clientInfoSheetRatedVOS.get(0));
            Member member = new Member();
            member.setMemberCustomerName(clientInfoSheetRatedVOS.get(0).getClientName());
            member.setMemberCustomerNo(clientInfoSheetRatedVOS.get(0).getClientNumber());
            member.setCertificateType(clientInfoSheetRatedVOS.get(0).getClinetDocumentsType());
            member.setIdentificationNum(clientInfoSheetRatedVOS.get(0).getClinetDocumentsNumber());
            member.setCreditAmount(String.valueOf(clientInfoSheetRatedVOS.get(0).getRatedCreditLimit()));
            member.setCurrency(mxzDTO.getCurrency());
            member.setDepositRatio(mxzDTO.getDepositRatio());
            member.setRegistrant(mxzDTO.getRegistrant());
            memberService.insertSelective(member);
        }

        return Result.ok();
    }

}
